Kurt Hsu's blog

The Rails developer in taiwan.


  • 首頁

  • 標籤

  • 分類

  • 歸檔

[Rails] I18n default scope

發表於 2020-05-26 更新於 2020-05-28 分類於 Rails , i18n

在挖 deivse set_flash_message code 的時候研究了很久, 把一些心得記錄下來
這篇就不把 locale 檔案一一打出來了, 只記錄一下最終字串結果, 並且架設目前語系是 zh

基本使用和特性

1
2
3
4
5
6
7
8
I18n.t(:hello)
I18n.t('hello')
I18n.t(:hello, :world)
I18n.t([:hello, :world])
I18n.t(['hello', 'world'])
# => zh.hello
I18n.t('hello.world')
# => zh.hello.world

scope

scope 像是 namespace 的感覺, 要注意是加在最前面

1
2
3
4
5
6
I18n.t(:cat, scope: :super)
I18n.t(:cat, scope: 'super')
# => zh.super.cat
I18n.t(:cat, scope: [:super, :power])
I18n.t(:cat, scope: [:super, 'power'])
# => zh.super.power.cat

default

default 就是當找不到 i18n 翻譯時回傳的東西, 如果給予 array 則找到東西為止

1
2
3
4
5
6
I18n.t(:man, default: 'I am Iron man')
# => 如果沒有找到 zh.man 的翻譯就回傳字串 'I am Iron man'
I18n.t(:man, default: [:super, :hero, 'I am Iron man'])
# => 依順序 zh.man > zh.super > zh.hero 的翻譯都沒有找到的話就回傳字串 'I am Iron man'
I18n.t(:man, default: [:super, :hero])
# => 同上依順序的翻譯都沒有找到的話就回傳字串 'translation missing: zh.man'

但要注意的是如果翻譯是有階層的話會回傳整個 hash, 例如:

1
2
3
4
zh:
man:
super:
hansome: '我是帥哥'

這時 I18n 的動作為:

1
2
I18n.t(:man, default: [:super, :hansome])
# => {:hansome=>'我是帥哥'}

因為是 hash 不是 nil 就會整包回傳過來了

scope 搭配 default

default 也會吃到 scope namespace

1
2
I18n.t(:man, scope: [:super, :iron], default: [:woman, 'I am Iron man'])
# => 依順序 zh.super.iron.man > zh.super.iron.woman 的翻譯都沒有找到的話就回傳字串 'I am Iron man'

小結

我還是覺得非必要不要寫太多的 scope 和 default 其實挺通靈的, 如果沒有把 devise 的 find_message binding 看裡面發生了什麼事情其實挺難懂的, 還是用最基本的寫法比較好收尋和維護

I18n 系列:

[Rails] I18n 基本使用] [Rails] I18n 語系設定]
# Rails # i18n
[Rails]Devise confirmable
[Rails] I18n 語系設定
  • 文章目錄
  • 本站概要

Kurt Hsu

Progress One Percent Every Day
171 文章
55 分類
163 標籤
RSS
  1. 1. 基本使用和特性
  2. 2. scope
  3. 3. default
  4. 4. scope 搭配 default
  5. 5. 小結
  6. 6. I18n 系列:
© 2020 Kurt Hsu
由 Hexo 強力驅動 v3.8.0
|
主題 – NexT.Muse v7.3.0